#include "mapscen.h"


MapScen::MapScen(QObject *parent) :
    QGraphicsScene(parent)
{

    setSceneRect(-9000,-9000,9000,9000);
    ppath =  QPainterPath();
    createEdge = false;
    scenStatus = addNode;

    connect(this,SIGNAL(mScenEvent(MouseEventType,QPointF)),this,SLOT(drawPaintCmd(MouseEventType,QPointF)));


}



void MapScen::drowPoint(MapScen::MouseEventType eventType, QPointF p)
{
    if(eventType == rbMouseRelease)
    {
        NodeItem *node = new NodeItem(p.x(),p.y(),30,30);
        addItem(node);
    }
}

void MapScen::drowLine(MapScen::MouseEventType eventType, QPointF p)
{
    if(eventType == rbMouseRelease){
        createEdge = false;

        QList<QGraphicsItem *> startItems = items(new_edge->line().p1());
        if (startItems.count() && startItems.first() == new_edge)
            startItems.removeFirst();
        QList<QGraphicsItem *> endItems = items(new_edge->line().p2());
        if (endItems.count() && endItems.first() == new_edge)
            endItems.removeFirst();

        removeItem(new_edge);
        delete new_edge;

      //  qDebug() << startItems.first()->type() << NodeItem::Type;

        while(!startItems.isEmpty() && !(startItems.first()->type() == NodeItem::Type))
        {

            startItems.removeFirst();
        }
        while(!endItems.isEmpty() && !(endItems.first()->type() == NodeItem::Type))
        {
            endItems.removeFirst();
        }

        if (startItems.count() > 0 && endItems.count() > 0 &&
                startItems.first()->type() == NodeItem::Type &&
                endItems.first()->type()   == NodeItem::Type &&
                startItems.first() != endItems.first())
        {
            NodeItem *startItem = qgraphicsitem_cast<NodeItem *>(startItems.first());
            NodeItem *endItem   = qgraphicsitem_cast<NodeItem *>(endItems.first());

            //qDebug()<< startItem->scenePos() <<  startItem->pos();
            EdgeItem *edge      = new EdgeItem(startItem,endItem);
            addItem(edge);





        }


    }else if(eventType == rbMousePress){
        startLinePoint = p;
        QLineF tmp_line =  QLineF(p,p);
        new_edge = new EdgeItem(tmp_line);
        addItem(new_edge);
        createEdge = true;

    }else if(eventType == mMove){
        if(createEdge){
            QLineF tmp_line =  QLineF(startLinePoint,p);
            new_edge->setLine(tmp_line);
        }
    }
}

void MapScen::loadMap()
{
}

void MapScen::addLayer()
{
}

void MapScen::drawPaintCmd(MapScen::MouseEventType eventType, QPointF p)
{
    switch(scenStatus){
    case addNode:
    {
        drowPoint(eventType,p);
    }
        break;
    case addEdge:
    {
        drowLine(eventType,p);
    }
        break;
    }


}

void MapScen::setScenStatus(int scStatus)
{
    switch(scStatus){
    case 0:
    {
        scenStatus = addNode;
    }
        break;
    case 1:
    {
        scenStatus = addEdge;
    }
        break;
    case 2:
    {
        scenStatus = viewScen;
    }
        break;

    }
}

//void MapScen::keyPressEvent(QKeyEvent *event)
//{
//    QGraphicsView::keyPressEvent(event);
//}

//void MapScen::drawBackground(QPainter *painter, const QRectF &rect)
//{

//}

void MapScen::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF p = event->scenePos();

    if(event->button() == Qt::RightButton){

        emit mScenEvent(rbMousePress, p);

    }else if (event->button() == Qt::LeftButton) {

        emit mScenEvent(lbMousePress, p);

    }

    QGraphicsScene::mousePressEvent(event);

}

void MapScen::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF p = event->scenePos();

    if(event->button() == Qt::RightButton){

        emit mScenEvent(rbMouseRelease, p);

    }else if (event->button() == Qt::LeftButton) {

        emit mScenEvent(lbMouseRelease, p);

    }

    QGraphicsScene:: mouseReleaseEvent(event);
}

void MapScen::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QPointF p = event->scenePos();
    emit mScenEvent(mMove,p);
    QGraphicsScene:: mouseMoveEvent(event);
}



//void MapScen::mouseReleaseEvent(QMouseEvent *event)
//{

//}

